import O1HD from "../../../engines/gameEngine";
import level_peopleArchery from "../level_peopleArchery";

const {ccclass, property} = cc._decorator;

@ccclass
export default class level_PeopleArcheryUnitBase extends cc.Component {
    /**血量 */
    protected _hp:number = 0;
    /**最大血量 */
    protected _maxHp:number = 0;
    /** 攻速*/
    protected _atkSpd:number = 0;
    /**攻速加成 */
    protected _atkSpdAdd:number = 0;
    /** 攻击力 */
    protected _atk:number = 0;
    /**移动速度 */
    protected _moveSpd:number = 0;
    /**最小攻击速度 */
    protected _minAtkSpd:number = 0.25;
    /**最大攻击速度 */
    protected _maxAtkSpd:number = 2;
    /**最小攻击力 */
    protected _minAtk:number = 0;
    /**每五秒回血量 */
    protected _revitalize:number = 0;
    /**射击伤害加成 */
    protected _atkAddition:number = 0;
    /**血量收益 */
    protected _hpBenefit:number = 1;
    /**撞击后恢复的血量 */
    protected _strikeRecover:number = 0;
    /**每局金币加成 */
    protected _goldAddition:number = 0;
    /**暴击几率 */
    protected _criticalPro:number = 0;
    /**暴击伤害加成 */
    protected _criticalAddition:number = 0.5;
    /**射击吸血率 */
    protected _shotLeech:number = 0;
    /**血量上限加成 */
    protected _maxHpAddition:number = 0;
    /**回血增强 */
    protected _revitalizeAddition:number = 0;
    /**减伤 */
    protected _reduceDamage:number = 0;
    /**boss血量降低 */
    protected _bossHpReduce:number = 0;
    /**每五秒回百分比血量 */
    protected _revitalizePercent:number = 0;
    /**击败boss时回复百分比血量 */
    protected _bossDeathRevitalizePercent:number = 0;
    /** 伤害加成2 */
    protected _atkAddition2:number = 1;

    /**是否死亡 */
    isDeath:boolean = false;
    /**获取当前血量 */
    get hp() {return this._hp;}
    /**设置当前血量 */
    set hp(hp:number){this._hp = Math.floor(Math.min(hp,this.maxHp));}

    /**获取最大生命值 */
    get maxHp() {
        //血量加成
        let addition = this._maxHp * this.maxHpAddition;
        return this._maxHp + addition;
    }
    /**获取真实的最大生命值 */
    get realMaxHp(){
        return this._maxHp;
    }
    /**设置最大生命值 */
    set maxHp(maxHp:number){this._maxHp = Math.floor(maxHp);}

    /**获取攻速 */
    get atkSpd() {return this._atkSpd;}
    /**设置攻速 */
    set atkSpd(atkSpd:number){this._atkSpd = atkSpd;}

    /**获取攻速加成 */
    get atkSpdAdd() {return this._atkSpdAdd;}
    /**设置攻速加成 */
    set atkSpdAdd(atkSpdAdd:number){this._atkSpdAdd = atkSpdAdd;}

    get shotAtk(){
        let atk:number = this._atk;
        let atkAddition:number = this.atkAddition;
        let addition:number = Math.floor( atkAddition * atk);
        let addition2:number = this.atkAddition2;
        let realAddition:number = (atk + addition) * addition2;
        return realAddition;
    }


    /**获得攻击力 */
    get atk() {
        let atk:number = this._atk;
        return atk;
    }
    /**设置攻击力 */
    set atk(atk:number){this._atk = Math.max(atk,this._minAtk);}

    /**移动速度 */
    get moveSpd() {return this._moveSpd;}
    /**设置移动速度 */
    set moveSpd(moveSpd:number){this._moveSpd = moveSpd;}

    /**获取最小攻击力 */
    get minAtk() {return this._minAtk;}
    /**获取最大攻击力 */
    set minAtk(minAtk:number){this._minAtk = minAtk;}

    /**获取最小攻速 */
    get minAtkSpd(){return this._minAtkSpd;}
    /**设置最小攻速 */
    set minAtkSpd(minAtkSpd:number){this._minAtkSpd = minAtkSpd;}

    /**获取最大攻速 */
    get maxAtkSpd(){return this._maxAtkSpd;}
    /**设置最大攻速 */
    set maxAtkSpd(maxAtkSpd:number){this._maxAtkSpd = maxAtkSpd;}

    /**获取五秒回复 */
    get revitAlize(){return this._revitalize;}
    /**设置五秒回复 */
    set revitAlize(revitalize:number){this._revitalize = Math.max(0,revitalize);}

    /**攻击加成 */
    get atkAddition(){return this._atkAddition;}
    /**设置攻击加成 */
    set atkAddition(atkAddition:number){this._atkAddition = Math.max(0,atkAddition);}

    /**攻击加成 */
    get atkAddition2(){return this._atkAddition2;}
    /**设置攻击加成 */
    set atkAddition2(atkAddition:number){this._atkAddition2 = Math.max(0,atkAddition);}

    /**获取血量收益 */
    get hpBenefit(){return this._hpBenefit;}
    /**设置血量收益 */
    set hpBenefit(hpBenefit:number){this._hpBenefit = Math.max(1,hpBenefit);}

    /**获取撞击回血 */
    get strikeRecover(){return this._strikeRecover;}
    /**设置撞击回血 */
    set strikeRecover(strikeRecover:number){this._strikeRecover = Math.max(0,strikeRecover);}

    /**获取金币加成 已完成 */
    get goldAddition(){return this._goldAddition;}
    /**设置金币加成 */
    set goldAddition(addition:number){this._goldAddition = Math.max(0,addition);}

    /** 获取暴击率 已完成 */
    get criticalPro(){return this._criticalPro;}
    /** 设置暴击率*/
    set criticalPro(criticalPro:number){this._criticalPro = Math.max(0,criticalPro);}

    /**获取暴击伤害加成 已完成 */
    get criticalAddition(){return this._criticalAddition;}
    /**设置暴击伤害加成 */
    set criticalAddition(criticalAddition:number){this._criticalAddition = Math.max(0,criticalAddition);}

    /**获取射击吸血率 已完成 */
    get shotLeech(){return this._shotLeech;}
    /**设置射击吸血率 */
    set shotLeech(shotLeech:number){this._shotLeech = Math.max(0,shotLeech);}

    /** 获取血量上限加成 已完成 */
    get maxHpAddition(){return this._maxHpAddition;}
    /** 设置血量上限加成 */
    set maxHpAddition(maxHpAddition:number){this._maxHpAddition = Math.max(0,maxHpAddition);}

    /**获取回血增强 已完成 */
    get revitalizeAddition(){return this._revitalizeAddition;}
    /**设置回血增强 */
    set revitalizeAddition(revitalizeAddition:number){this._revitalizeAddition = Math.max(0,revitalizeAddition);}
    
    /**获取伤害减免 已完成 */
    get reduceDamage(){return this._reduceDamage;}
    /** 设置伤害减免 */
    set reduceDamage(reduceDamage:number){this._reduceDamage = Math.max(0,reduceDamage);}
    
    /**获取boss血量降低 已完成 */
    get bossHpReduce(){return this._bossHpReduce;}
    /** 设置boss血量降低 */
    set bossHpReduce(bossHpReduce:number){this._bossHpReduce = Math.max(0,bossHpReduce);}
    
    /**获取每五秒回百分比血量 已完成 */
    get revitalizePercent(){return this._revitalizePercent;}
    /**设置每五秒回百分比血量 */
    set revitalizePercent(revitalizePercent:number){this._revitalizePercent = Math.max(0,revitalizePercent);}

    /**获取击败boss时回复百分比血量 */
    get bossDeathRevitalizePercent(){return this._bossDeathRevitalizePercent;}
    /**设置击败boss时回复百分比血量 */
    set bossDeathRevitalizePercent(bossDeathRevitalizePercent:number){this._bossDeathRevitalizePercent = Math.max(0,bossDeathRevitalizePercent);}

    level:level_peopleArchery = null;

    /** 两秒回复计时 */
    private revitAlizeTime:number = 0;
    /** 五秒回复计时 */
    private revitAlizePercentTime:number = 0;
    /**当前的所有法宝 */
    protected _weapons:string[] = [];

    /**绑定关卡 */
    bindLevel(level:level_peopleArchery){
        this.level = level;
    }

    /**
     * 单位受到伤害 
     * @param atk 伤害
     * @param atkType 伤害类型 0是弓箭伤害 1是撞击伤害
    */
    async unitBeHurt(atk:number,atkType?:string,criticalFlag?:boolean){
        //计算伤害减免后的伤害
        atk -= Math.floor(atk * this.reduceDamage);
        let hp = this.hp;
        atk = Math.floor(atk);
        let lastHp:number = hp - atk;
        this.level.addSubLb(atk,this.node,criticalFlag);
        this.hp = Math.max(0,lastHp);
        O1HD.getManagers().soundMgr.playSound("peopleArcherySound/hit");
        O1HD.getManagers().soundMgr.playVibrate();
        if(lastHp <= 0){
            // 死亡
            await this.unitDeath();
        }else{
            this.unitBeHurtNotDieFun(atkType);
            await this.unitBeHurtAni();
        }
    }

    /**
     * 单位未受到致命伤害
     * @param atkType 伤害类型
    */
    unitBeHurtNotDieFun(atkType:string){
        switch(atkType){
            case "0":{

                break;
            }
            case "1":{
                if(this.strikeRecover > 0){
                    let addHp:number = this.strikeRecover;
                    if(addHp > 0){
                        // 撞击回血
                        this.level.parseDataAddPeople({param_type:"0",option_param:addHp.toString(),reCoverMaxHp:"0"});
                    }
                }
                break;
            }
        }
    }

    /**
     * 单位受到致命伤
     * @param atk 伤害值
     * @param atkType 伤害类型
     */
    unitBeHurtDie(atk:number,atkType:string){
        switch(atkType){
            case "0":{

                break;
            }
            case "1":{

                break;
            }
        }
    }

    /**单位受到伤害动画 */
    async unitBeHurtAni(){

    }

    /** 单位死亡 */
    async unitDeath(){
        this.isDeath = true;
        this.node.getComponent(cc.Collider).enabled = false;
        await this.unitDeathAni();
        this.unitDestroy();
    }

    /**单位死亡动画 */
    async unitDeathAni(){
        return new Promise<void>(resolve=>{
            cc.tween(this.node).to(0.2,{opacity:0}).call(resolve).start();
        });
    }

    /**销毁单位 */
    unitDestroy(){
        this.node.destroy();
        this.node.removeFromParent();
    }

    /**碰撞开始 */
    onCollisionEnter(other:cc.Collider, self:cc.Collider) {

    }

    /**持续碰撞 */
    onCollisionStay(other:cc.Collider, self:cc.Collider) {

    }

    /**碰撞结束 */
    onCollisionExit(other:cc.Collider, self:cc.Collider) {

    }

    update(dt: number): void {
        let tIndex:number = this.level.unitContent.y - this.node.y; 
        tIndex = Math.min(tIndex,cc.macro.MAX_ZINDEX);
        tIndex = Math.max(tIndex,cc.macro.MIN_ZINDEX);
        this.node.zIndex = tIndex;
        if(this.level.gamePauseFlag || this.isDeath){
            return;
        }
        this.revitAlizeTime += dt;
        if(this.revitAlizeTime >= 2){
            // 每2秒回血
            this.revitAlizeTime = 0;
            if(this.revitAlize > 0){
                let revitAlize = this.revitAlize;
                let revitalizeAddition = this.revitalizeAddition;
                let addition:number = revitAlize * revitalizeAddition;
                let data = {param_type:"0",option_param: revitAlize + addition,reCoverMaxHp:"0"};
                this.level.parseDataAddPeople(data);
            }
        }
        this.revitAlizePercentTime += dt;
        if(this.revitAlizePercentTime >= 5){
            // 每五秒回血
            this.revitAlizePercentTime = 0;
            if(this.revitalizePercent > 0){
                let revitAlizeHp:number = this.revitalizePercent * this.maxHp;
                let data = {param_type:"0",option_param:revitAlizeHp,reCoverMaxHp:"0"};
                this.level.parseDataAddPeople(data);
            }   
        }
    }

    autoDeath(){
        if(this.node.y < -1961){
            this.unitDestroy();
        }
    }

    addOneWeapon(weaponId:string){
        this._weapons.push(weaponId);
    }
}
